<?php

namespace lico\MVC;

use lico\Runtime\Config;
use lico\Runtime\Route;
use lico\Tool\Exception\SmartyException;
use \Smarty;

use lico\Container;

class View
{

    protected $data;
    protected $smarty;
    protected $errorCode = 0;
    protected $templateDir;

    public function __construct()
    {
        
    }

    /**
     * 设置数据
     */
    public function setData($key, $data = "")
    {
        if (!$data) {
            $this->data = $key;
        } else {
            $this->data[$key] = $data;
        }
    }

    /**
     * 返回数据
     */
    public function getData($key = "")
    {
        if (!$key) {
            $data = $this->data;
        } else {
            $data = $this->data[$key];
        }
        return $data;
    }

    /**
     * smarty初始化
     */
    public function smartyInit()
    {

        if (is_null($this->smarty)) {
            $this->smarty = new Smarty();
        }
        $appConfig = Container::getBowl('lico\Runtime\Config')->get('appConfig');
        $smartyConfig = $appConfig['smarty'];

        if (!$this->errorCode) {
            !empty($smartyConfig['left_delimiter']) && $this->smarty->setLeftDelimiter($smartyConfig['left_delimiter']);
            !empty($smartyConfig['right_delimiter']) && $this->smarty->setRightDelimiter($smartyConfig['right_delimiter']);
            $app = \lico\Container::getBowl('lico\Runtime\Route')->app();
            $module = \lico\Container::getBowl('lico\Runtime\Route')->module();
            $this->templateDir = ROOT_PATH . $app . DS . $smartyConfig['templateDir'] . DS . $module;
        } else {
            $defaultConfig = Container::getBowl('lico\Runtime\Config')->def('smarty');
            $this->smarty->setLeftDelimiter($defaultConfig['left_delimiter']);
            $this->smarty->setRightDelimiter($defaultConfig['right_delimiter']);
            $this->templateDir = LICO_PATH . 'Tool' . DS . 'Exception' . DS . 'html' . DS;
        }

        !empty($this->templateDir) && $this->smarty->setTemplateDir($this->templateDir); //设置模板目录
        !empty($smartyConfig['compileDir']) && $this->smarty->setCompileDir(ROOT_PATH . $smartyConfig['compileDir']); //设置编译目录
        !empty($smartyConfig['configDir']) && $this->smarty->setConfigDir(ROOT_PATH . $smartyConfig['configDir']);
        !empty($smartyConfig['cacheDir']) && $this->smarty->setCacheDir(ROOT_PATH . $smartyConfig['cacheDir']);
        // pe($smartyConfig,$this->smarty->getLeftDelimiter());
        //静态资源路径
        $public = constant('SYSCONFIG')['Public'];
        $staticUrl = '/' . $public['main'] . '/' . $public['static'];
        $this->setData('_STATIC_', $staticUrl);
        // p($staticUrl);
        //模版公共可使用参数
        foreach ($appConfig['template'] as $key => $value) {
            $this->setData($key, $staticUrl . $value);
        }
    }

    /**
     * smarty渲染
     */
    public function smartyView($html)
    {

        $templetehtml = $html . '.html';

        $this->setData('lico_html_realurl', $templetehtml);
        $this->smartyInit();
        $data = $this->getData();
        // p($data);
        foreach ($data as $key => $value) {
            if ($key != 'lico_html_realurl') {
                $this->smarty->assign($key, $value);
            }
        }

        // ob_start();
        ob_implicit_flush(0);
        try {
            $this->smarty->display($data['lico_html_realurl']);
        } catch (\Exception $e) {
            throw new SmartyException($e->getMessage(), 1);
        }
    }

    /**
     * smarty错误显示
     */
    public function smartyError($message)
    {

        $this->errorCode = 1;
        $this->setData([]);
        $this->setData('error', $message);
        $this->smartyView('smartyError');
    }
}
